package com.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapper.auto.SchoolIntroductionMapper;
import com.model.auto.*;
import com.model.auto.resp.AdmissionSchoolResp;
import com.service.*;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2022-09-14
 */
@Service
public class SchoolIntroductionServiceImpl extends ServiceImpl<SchoolIntroductionMapper, SchoolIntroduction> implements ISchoolIntroductionService {

    @Autowired
    private ISchoolLabelService schoolLabelService;
    @Autowired
    private ISchoolService schoolService;
    @Autowired
    private ISchoolV2Service schoolV2Service;
    @Autowired
    private IMajorV2Service majorV2Service;

    @Override
    public SchoolIntroduction selectOneBySchoolCode(String code, String schoolName) {
        return baseMapper.selectOneBySchoolCode(code, schoolName);
    }

    @Override
    public SchoolIntroduction selectByCode(String code, String schoolName) {
        SchoolIntroduction schoolIntroduction = baseMapper.selectOneBySchoolCode(code, schoolName);
        if(StringUtil.isEmpty(code)){
            code = schoolIntroduction.getSchoolCode();
        }
        if(!ObjectUtils.isEmpty(schoolIntroduction)){
            //获取学校标签
            SchoolLabel schoolLabel = schoolLabelService.selectByCode(code);
            if(!ObjectUtils.isEmpty(schoolLabel)){
                schoolIntroduction.setCity(schoolLabel.getCity());
                schoolIntroduction.setLevel(schoolLabel.getLevel());
                schoolIntroduction.setNature(schoolLabel.getNature());
                schoolIntroduction.setCollegeLevel(schoolLabel.getCollegeLevel());
                schoolIntroduction.setQualifications(schoolLabel.getQualifications());
                schoolIntroduction.setSchoolSort(schoolLabel.getSchoolSort());
                try{
                    if(!StringUtil.isEmpty(schoolLabel.getSchoolSort())){
                        schoolIntroduction.setSchoolSortJson(JSON.parseObject(schoolLabel.getSchoolSortJson(), LinkedHashMap.class));
                    }
                }catch (Exception e){
                    log.error("格式化学校排名json异常");
                }
            }
            //获取学校录取数据
            List<School> schools=schoolService.list(new LambdaQueryWrapper<School>()
                    .eq(School::getCode,code)
                    .orderByAsc(School::getBatch).orderByAsc(School::getType)
            );
            if(!ObjectUtils.isEmpty(schools)){
                schoolIntroduction.setManager(schools.get(0).getManager());
                schoolIntroduction.setSchoolType(schools.get(0).getSchoolType());
            }
            Map<String, List<School>> collect = schools.stream().collect(Collectors.groupingBy(v -> v.getBatch()));
            List<AdmissionSchoolResp> admissionSchoolList = new ArrayList<>();
            collect.forEach((key, value) -> {
                AdmissionSchoolResp admissionSchool = new AdmissionSchoolResp();
                admissionSchool.setBatch(key);
                admissionSchool.setCode(value.get(0).getCode());
                admissionSchool.setAdmissionData(value);
                admissionSchoolList.add(admissionSchool);
            });
            schoolIntroduction.setAdmissionSchool(admissionSchoolList);
        }
        return schoolIntroduction;
    }

    @Override
    public SchoolIntroduction selectByCodeV2(String code, String schoolName) {
        SchoolIntroduction schoolIntroduction = baseMapper.selectOneBySchoolCode(code, schoolName);
        if(StringUtil.isEmpty(code)){
            code = schoolIntroduction.getSchoolCode();
        }
        if(!ObjectUtils.isEmpty(schoolIntroduction)){
            //获取学校标签
            SchoolLabel schoolLabel = schoolLabelService.selectByCode(code);
            if(!ObjectUtils.isEmpty(schoolLabel)){
                schoolIntroduction.setCity(schoolLabel.getCity());
                schoolIntroduction.setLevel(schoolLabel.getLevel());
                schoolIntroduction.setNature(schoolLabel.getNature());
                schoolIntroduction.setCollegeLevel(schoolLabel.getCollegeLevel());
                schoolIntroduction.setQualifications(schoolLabel.getQualifications());
                schoolIntroduction.setSchoolSort(schoolLabel.getSchoolSort());
                try{
                    if(!StringUtil.isEmpty(schoolLabel.getSchoolSort())){
                        schoolIntroduction.setSchoolSortJson(JSON.parseObject(schoolLabel.getSchoolSortJson(), LinkedHashMap.class));
                    }
                }catch (Exception e){
                    log.error("格式化学校排名json异常");
                }
            }
            //获取学校录取数据
            List<SchoolV2> schools=schoolV2Service.list(new LambdaQueryWrapper<SchoolV2>()
                    .eq(SchoolV2::getCode,code)
                    .orderByAsc(SchoolV2::getBatch).orderByAsc(SchoolV2::getType)
            );
            if(!ObjectUtils.isEmpty(schools)){

                schools.forEach(schoolV2 -> {
                    if(StringUtils.hasText(schoolV2.getManager())){
                        schoolIntroduction.setManager(schoolV2.getManager());
                    }
                    if(StringUtils.hasText(schoolV2.getSchoolType())){
                        schoolIntroduction.setSchoolType(schoolV2.getSchoolType());
                    }
                    List<MajorV2> list = majorV2Service.list(new LambdaQueryWrapper<MajorV2>()
                            .eq(MajorV2::getCode, schoolV2.getCode())
                            .eq(MajorV2::getType, schoolV2.getType())
                            .eq(MajorV2::getGroupCode, schoolV2.getGroupCode())
                            .eq(MajorV2::getBatch, schoolV2.getBatch())
                    );
                    schoolV2.setMajorV2List(list);
                });
            }
            Map<String, List<SchoolV2>> collect = schools.stream().collect(Collectors.groupingBy(v -> v.getBatch()));
            List<AdmissionSchoolResp> admissionSchoolList = new ArrayList<>();
            collect.forEach((key, value) -> {
                AdmissionSchoolResp admissionSchool = new AdmissionSchoolResp();
                admissionSchool.setBatch(key);
                admissionSchool.setCode(value.get(0).getCode());
                admissionSchool.setAdmissionDataV2(value);
                admissionSchoolList.add(admissionSchool);
            });
            schoolIntroduction.setAdmissionSchool(admissionSchoolList);
        }
        return schoolIntroduction;
    }
}
